/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <QPainter>
#include <QPixmapCache>
#include "threaditemdelegate.h"
#include "threaditemmodel.h"
#include "log.h"

ThreadItemDelegate::ThreadItemDelegate(QObject* parent) : QStyledItemDelegate(parent),
	m_picMode(true)
{
	m_dead.load(":/wishmaster/images/skull.png");
	m_dead = m_dead.scaled(16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation);

	m_watched.load(":/wishmaster/images/save.png");
	m_watched = m_watched.scaled(16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation);

	m_responded.load(":/wishmaster/images/reply.png");
	m_responded = m_responded.scaled(16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

ThreadItemDelegate::~ThreadItemDelegate()
{
}

QSize ThreadItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	if(!index.isValid())
		return QSize(0, 0);
	QFontMetrics metrics(option.font);
	if(!m_picMode)
	{
		int offset = 0;
		int flags = index.data(ThreadItemModel::ThreadFlags).toUInt();
		if(flags & ThreadItemModel::ThreadDead)
			offset += 16;
		if(flags & ThreadItemModel::ThreadWatch)
			offset += 16;
		if(flags & ThreadItemModel::ThreadResponded)
			offset += 16;
		QString s = index.data(Qt::DisplayRole).toString();
		s.replace("\n", " ");
		QRect r = metrics.boundingRect(s);
		return QSize(r.width() + offset, metrics.height());
	}
	else
	{
		QString s = index.data(Qt::DisplayRole).toString();
		int height;
		if(!index.data(Qt::DecorationRole).value<QImage>().isNull())
			height = PicSize + 4;
		else
			height = std::min(metrics.lineSpacing() * (s.count("\n") + 1) + 4, PicSize + 4);
		QRect r = metrics.boundingRect(s);
		return QSize(r.width(), height);
	}
}

bool ThreadItemDelegate::helpEvent(QHelpEvent* event, QAbstractItemView* view, const QStyleOptionViewItem& option, const QModelIndex& index)
{
	return true;
}

void ThreadItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	if(index.column() == 0)
	{
		QStyleOptionViewItemV4 opt(option);
		initStyleOption(&opt, index);

		painter->save();
		QStyle *style = opt.widget->style();
		style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);

		if (option.state & QStyle::State_Selected)
			painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));

		//QRect itemRect = option.rect.adjusted(2, 2, -2, -2);
		QRect r; // = QStyle::alignedRect(opt.direction, Qt::AlignTop | Qt::AlignHCenter, opt.decorationSize, itemRect);

		int flags = index.data(ThreadItemModel::ThreadFlags).toUInt();
		if(!m_picMode)
		{
			int offset = 0;
			r = option.rect;
			r.setWidth(16); //FIXME hardcoded
			r.setHeight(16); //FIXME hardcoded
			r.translate(0, option.rect.height() / 2 - 8);
			if(flags & ThreadItemModel::ThreadDead)
			{
				painter->drawPixmap(r, m_dead);
				offset += 16; //FIXME hardcoded
				r.translate(16, 0); //FIXME hardcoded
			}
			if(flags & ThreadItemModel::ThreadWatch)
			{
				painter->drawPixmap(r, m_watched);
				offset += 16;
				r.translate(16, 0);
			}
			if(flags & ThreadItemModel::ThreadResponded)
			{
				painter->drawPixmap(r, m_responded);
				offset += 16;
				r.translate(16, 0);
			}
			r = option.rect;
			r.setLeft(offset);

			painter->setFont(index.data(Qt::FontRole).value<QFont>());
			if (option.state & QStyle::State_Selected)
				painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));
			else
				painter->setPen(index.data(Qt::ForegroundRole).value<QColor>());

			QString s = index.data(Qt::DisplayRole).toString();
			s.replace("\n", " ");
			painter->drawText(r, Qt::AlignVCenter | Qt::AlignLeft, s);
		}
		else
		{
			r = option.rect;
			r.setWidth(16); //FIXME hardcoded
			r.setHeight(16); //FIXME hardcoded
			r.translate(0, 0);
			int iconsCnt = 0;
			if(flags & ThreadItemModel::ThreadDead)
			{
				painter->drawPixmap(r, m_dead);
				r.translate(0, 16); //FIXME hardcoded
				iconsCnt++;
			}
			if(flags & ThreadItemModel::ThreadWatch)
			{
				painter->drawPixmap(r, m_watched);
				r.translate(0, 16); //FIXME hardcoded
				iconsCnt++;
			}
			if(flags & ThreadItemModel::ThreadResponded)
			{
				painter->drawPixmap(r, m_responded);
				r.translate(0, 16); //FIXME hardcoded
				iconsCnt++;
			}
			r = option.rect;
			r.adjust(16, 2, 0, -2);
			QString pixmapid = index.data(ThreadItemModel::UniqueIdRole).toString();
			QPixmap p;
			if(!QPixmapCache::find(pixmapid, &p))
			{
				p = QPixmap::fromImage(index.data(Qt::DecorationRole).value<QImage>());
				if(!p.isNull())
				{
					p = p.scaled(QSize(PicSize, PicSize), Qt::KeepAspectRatio, Qt::SmoothTransformation);
					QPixmapCache::insert(pixmapid, p);
				}
			}
			r.setWidth(p.width());
			r.setHeight(p.height());
			r.translate((PicSize - r.width()) / 2, (PicSize - r.height()) / 2);
			painter->drawPixmap(r, p);

			r = option.rect;
			if(iconsCnt)
				r.adjust(16, 0, 0, 0);
			if(!p.isNull())
				r.adjust(16 + PicSize + 2, 0, 0, 0);
			painter->setFont(index.data(Qt::FontRole).value<QFont>());
			if (option.state & QStyle::State_Selected)
				painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));
			else
				painter->setPen(index.data(Qt::ForegroundRole).value<QColor>());

			QString s = index.data(Qt::DisplayRole).toString().section('\n', 0, 3, QString::SectionSkipEmpty); // FIXME hardcoded
			painter->drawText(r, Qt::AlignTop | Qt::AlignLeft, s);
		}
		painter->restore();
	}
}

